using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using Cards.GUI;
using Cards.Objects;

namespace Cards.Util
{
    public class CheckRule_BlackJack
    {
        public static int EvaluateScore(INS_Player _player)
        {
            int rs = 0;
            for (int i = 0; i < _player.IC.Length; i++)
            {
                if ((_player.IC[i] != null) && (_player.IC[i].CARD.CARDTYPE != -1))
                {
                    if (_player.IC[i].CARD.CARDVALUE == Constants.GT_ACH)
                    {
                        if (_player.GetRealLength() < 3)
                        {
                            rs += _player.IC[i].CARD.REALVALUE;
                        }
                        else if (_player.GetRealLength() == 3)
                        {
                            if (GetTotalScoreWithAce10(_player) > 21)
                            {
                                rs += 1;
                            }
                            else
                            {
                                rs += _player.IC[i].CARD.REALVALUE - 1;
                            }
                        }
                        else
                        {
                            rs += 1;
                        }
                    }
                    else
                    {
                        rs += _player.IC[i].CARD.REALVALUE;
                    }
                }
            }
            return rs;
        }

        public static int GetTotalScoreWithAce10(INS_Player _player)
        {
            int rs = 0;
            for (int i = 0; i < _player.IC.Length; i++)
            {
                if ((_player.IC[i] != null) && (_player.IC[i].CARD.CARDTYPE != -1))
                {
                    if (_player.IC[i].CARD.CARDVALUE == Constants.GT_ACH)
                    {
                        rs += Constants.GT_BLACKJACK_REALVALUE;
                    }
                    else
                    {
                        rs += _player.IC[i].CARD.REALVALUE;
                    }
                }
            }
            return rs;
        }

        public static Boolean CheckEnoughScore(INS_Player _player)
        {
            if ((((INS_BlackJackPlayer)_player).BANKER && EvaluateScore(_player) >= 15)
                || ((!((INS_BlackJackPlayer)_player).BANKER) && EvaluateScore(_player) >= 16))
            {
                return true;
            }
            return false;
        }

        public static Boolean CheckGoodScore(INS_Player _player)
        {
            if ((((INS_BlackJackPlayer)_player).BANKER && EvaluateScore(_player) >= 15 && EvaluateScore(_player) < 22)
                || (!((INS_BlackJackPlayer)_player).BANKER && EvaluateScore(_player) >= 16 && EvaluateScore(_player) < 22))
            {
                return true;
            }
            return false;
        }

        public static Boolean CheckGoodScoreToContinue(INS_Player _player)
        {
            if (EvaluateScore(_player) >= 18 && EvaluateScore(_player) < 22)
            {
                return true;
            }

            if (((INS_BlackJackPlayer)_player).BANKER)
            {
                if (EvaluateScore(_player) >= 15 && EvaluateScore(_player) < 18)
                {
                    Random rand = new Random();
                    int ir = rand.Next(2);

                    if (ir == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else
            {
                if (EvaluateScore(_player) >= 16 && EvaluateScore(_player) < 18)
                {
                    Random rand = new Random();
                    int ir = rand.Next(2);

                    if (ir == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
                
            return false;
        }

        public static Boolean CheckBadOutScore(INS_Player _player)
        {
            if (EvaluateScore(_player) > 21)
            {
                return true;
            }
            return false;
        }

        public static Boolean CheckAceJack(INS_Player _player)
        {
            if (((INS_BlackJackPlayer)_player).INDEX == 2)
            {
                if (((_player.IC[0].CARD.CARDVALUE == Constants.GT_ACH)||
                     (_player.IC[1].CARD.CARDVALUE == Constants.GT_ACH))
                    && ((_player.IC[0].CARD.REALVALUE == Constants.GT_BLACKJACK_REALVALUE)||
                        (_player.IC[1].CARD.REALVALUE == Constants.GT_BLACKJACK_REALVALUE)))
                {
                    return true;
                }
            }
            return false;
        }

        public static Boolean CheckDoubleAceJack(INS_Player _player)
        {
            if (((INS_BlackJackPlayer)_player).INDEX == 2)
            {
                if ((_player.IC[0].CARD.CARDVALUE == Constants.GT_ACH)
                    && (_player.IC[1].CARD.CARDVALUE == Constants.GT_ACH))
                {
                    return true;
                }
            }
            return false;
        }

        public static Boolean CheckGood5Cards(INS_Player _player)
        {
            if (((INS_BlackJackPlayer)_player).INDEX == _player.MAXNUMOFCARDS)
            {
                if (CheckGoodScore(_player))
                {
                    return true;
                }
            }
            return false;
        }

        public static BLACKJACK_RANK GetRank(INS_Player _player)
        {
            if (CheckDoubleAceJack(_player))
            {
                return BLACKJACK_RANK.R_DOUBLE_ACE_JACK;
            }
            else if (CheckAceJack(_player))
            {
                return BLACKJACK_RANK.R_ACE_JACK;
            }
            else if (CheckGood5Cards(_player))
            {
                return BLACKJACK_RANK.R_FIVE_CARDS;
            }
            else if (CheckEnoughScore(_player) && (EvaluateScore(_player) == 21))
            {
                return BLACKJACK_RANK.R_TWENTY_FIRST;
            }
            else if (CheckEnoughScore(_player) && (EvaluateScore(_player) < 21))
            {
                return BLACKJACK_RANK.R_AVERAGE;
            }
            else if (CheckBadOutScore(_player) || !CheckEnoughScore(_player))
            {
                return BLACKJACK_RANK.R_OUT_RANGE;
            }

            return BLACKJACK_RANK.R_UNKNOWN;
        }
    }
}
